package cn.edu.jlu.community.app.post.forum;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.edu.jlu.community.R;
import cn.edu.jlu.community.app.base.ToolbarActivity;
import cn.edu.jlu.community.server.AppServer;
import cn.edu.jlu.community.util.ToastUtil;

public class NewPostActivity extends ToolbarActivity
        implements AppServer.PushCallback {

    @InjectView(R.id.title)
    EditText titleEdit;
    @InjectView(R.id.content)
    EditText contentEdit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ButterKnife.inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.forum_new_post_activity;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_new_post, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_send) {
            String title = titleEdit.getText().toString();
            String content = contentEdit.getText().toString();
            if (title.isEmpty()) {
                ToastUtil.showText("标题不能为空");
            } else if (content.isEmpty()) {
                ToastUtil.showText("内容不能为空");
            } else {
                AppServer.newPost(title, content, this);
            }

            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onSuccess() {
        ToastUtil.showText("发表成功");
        finish();
    }
}
